package com.yugao.fintech.antelope.auth.model.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yugao.fintech.antelope.base.model.BasePO;
import com.yugao.fintech.antelope.base.model.constants.UserCons;
import com.yugao.fintech.antelope.base.model.constants.ValidationCons;
import com.yugao.fintech.framework.desensitization.annotations.FieldSensitive;
import com.yugao.fintech.framework.desensitization.fieldsensitive.enums.StrategyType;
import com.yugao.fintech.framework.core.annotations.FieldUnique;
import org.springframework.format.annotation.DateTimeFormat;
import lombok.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 本地登录-自己用户名、密码登录，手机号码登录信息记录(目前没有使用, 可能后期会使用)
 */
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("auth_local_user")
public class LocalUser extends BasePO {
    @TableId
    private Long authId;

    /**
     * 用户名
     */
    @FieldUnique(message = "用户名已存在")
    private String userName;

    /**
     * 用户id
     */
    @NotNull(message = ValidationCons.NOT_EMPTY)
    private Long userId;

    /**
     * 密码
     */
    @FieldSensitive(strategy = StrategyType.EMPTY)
    private String password;

    /**
     * 手机号
     */
    private String phone;

    /**
     * 邮箱
     */
    @FieldSensitive(strategy = StrategyType.EMAIL)
    private String email;

    /**
     * @see UserCons.Status
     */
    private String status;

    /**
     * 应用类型
     *
     * @see com.yugao.fintech.antelope.base.model.constants.AppType
     */
    @NotEmpty(message = ValidationCons.NOT_EMPTY)
    private String appType;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createdAt;

    /**
     * 更新时间
     */
    @TableField(fill = FieldFill.UPDATE)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updatedAt;


    public static LambdaQueryWrapper<LocalUser> lqw() {
        return Wrappers.lambdaQuery();
    }

    public boolean isAdmin() {
        return isAdmin(this.userId);
    }

    public static boolean isAdmin(Long userId) {
        return UserCons.ADMIN_ID.equals(userId);
    }
}
